<% layout('/layouts/default.html', {title: '模板管理管理', libs: ['validate','fileupload','dataGrid']}){ %>

<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(ocrTemplate.isNewRecord ? '新增模板管理' : '编辑模板管理')}
			</div>
			<div class="box-tools pull-right hide">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${ocrTemplate}" action="${ctx}/ocr/ocrTemplate/save" method="post" class="form-horizontal">
			<div class="box-body">
				<div class="form-unit">${text('基本信息')}</div>
				<#form:hidden path="ocrTemplateId"/>
				<#form:hidden path="tempateImageId"/>
				
				<div class="row">
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('模板分类')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								
								<#form:treeselect id="ocrTempateCategory" title="${text('模板分类选择')}"
									path="ocrTempateCategory.tempateCategoryCode" labelPath="ocrTempateCategory.tempateCategoryName"
									url="${ctx}/ocr/ocrTempateCategory/treeData"
									class=" required" allowClear="true"/>
							</div>
						</div>
					</div>
					
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span><a onclick="js.windowOpen('${ctx}/ocr/ocrAlgorithmConfig/form', '新增算法', 1000, 800);">新增</a> ${text('算法配置')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
							
								<#form:listselect  title="算法选择"
									path="ocrAlgorithmConfig" labelPath="ocrAlgorithmConfig.name"
									url="${ctx}/ocr/ocrAlgorithmConfig/ocrAlgorithmConfigSelect" allowClear="true" 
									checkbox="false" itemCode="algorithmConfigId" itemName="name"/>											
									
									
							</div>
						</div>
					</div>					
					
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required ">*</span> ${text('模板名称')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:input path="name" maxlength="200" class="form-control required"/>
							</div>
						</div>
					</div>
					<div class="col-xs-6">
						<div class="form-group">
							<label class="control-label col-sm-4" title="">
								<span class="required hide">*</span> ${text('标签')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-8">
								<#form:listselect  title="标签选择"
									path="ocrTags" labelPath="ocrTags.tagName"
									url="${ctx}/ocr/ocrTags/ocrTagsSelect" allowClear="true" 
									checkbox="true" itemCode="tagName" itemName="tagName"/>										
																	
							</div>
						</div>
					</div>
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('模板介绍')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="description" rows="4" maxlength="500" class="form-control"/>
							</div>
						</div>
					</div>
				
				</div>
				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2" title="">
								<span class="required hide">*</span> ${text('模型提示词')}：<i class="fa icon-question hide"></i></label>
							<div class="col-sm-10">
								<#form:textarea path="llmPrompt" rows="4" maxlength="1000" class="form-control"/>
							</div>
						</div>
					</div>
				</div>


				<div class="row">
					<div class="col-xs-12">
						<div class="form-group">
							<label class="control-label col-sm-2">
								<span class="required hide">*</span> ${text('图片上传')}：</label>
							<div class="col-sm-10">
								<#form:fileupload cueWords="请选择一张清晰的包含内容的图片作为样本模板！" id="uploadImage" 	returnPath="true" 
									filePathInputId="tempateImage" bizKey="${ocrTemplate.id}" bizType="ocrTemplate_image"
									uploadType="image" class="" readonly="false" preview="true" maxUploadNum="1" dataMap="true"/>
								<#form:hidden  path="tempateImage"/>
							</div>
						</div>
					</div>
				</div>
				<div class="form-unit-wrap table-form">
						<a onclick="addRowTemplateRule();" class="btn btn-primary btn-sm mt10 mb10"><i class="icon-social-dribbble"></i> ${text('解析特征')}</a>
						<a onclick="deleteALLGrid();" class="btn btn-primary btn-sm mt10 mb10"><i class="icon-trash"></i> ${text('清空特征')}</a>
				</div>
				<div class="form-unit">${text('特征规则配置')} </div>
	
				
				<div class="row" id="imageToRecImageDiv">
				
				</div>
				 <hr/>
				
				<div class="form-unit-wrap table-form">
					<table id="ocrTempateConfigsDataGrid"></table>
					<% if (hasPermi('ocr:ocrTemplate:edit')){ %>
						<a href="#" id="ocrTempateConfigsDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
					<% } %>
				</div>
			</div>
			<div class="box-footer">
				<div class="row">
					<div class="col-sm-offset-2 col-sm-10">
						<% if (hasPermi('ocr:ocrTemplate:edit')){ %>
							<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
						<% } %>
						<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
					</div>
				</div>
			</div>
		</#form:form>
	</div>
</div>
<% } %>
<script>
//# // 初始化模板配置DataGrid对象
$('#ocrTempateConfigsDataGrid').dataGrid({
	data: "#{toJson(ocrTemplate.ocrTempateConfigsList)}",
	datatype: 'local', // 设置本地数据
	autoGridHeight: function(){return 'auto'}, // 设置自动高度
	//# // 设置数据表格列
	columnModel: [
		{header:'状态', name:'status', editable:true, hidden:true},
		{header:'主键', name:'ocrTempateConfigsId', editable:true, hidden:true},
		{header:'${text("索引号")}', name:'ocrNo', width:80, editable:true, hidden:true},
		{header:'${text("模板编码")}', name:'ocrTemplateId.ocrTemplateId', editable:true, hidden:true},
		{header:'${text("ocr_text")}', name:'ocrText', width:150,editable:true, hidden:true},
		{header:'${text("char_scores")}', name:'charScores', width:150,editable:true, hidden:true},
		{header:'${text("crnn_time")}', name:'crnnTime', width:150, editable:true, hidden:true},
		{header:'${text("block_time")}', name:'blockTime', width:150,editable:true, hidden:true},
		{header:'${text("box_score")}', name:'boxScore', width:150,editable:true, hidden:true},
		{header:'${text("angle_index")}', name:'angleIndex', width:150,editable:true, hidden:true},
		{header:'${text("angle_score")}', name:'angleScore', width:150,editable:true, hidden:true},
		{header:'${text("angle_time")}', name:'angleTime', width:150, editable:true, hidden:true},
		{header:'${text("box_point")}', name:'boxPoint', width:150,editable:true, hidden:true},		
		{header:'${text("特征项")}', name:'checkItems', width:100, 
			editable:true, edittype:'radio', editoptions:{'class':'form-control digits icheck',
				items: $.merge([], "#{@DictUtils.getDictListJson('sys_yes_no')}"),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					js.iCheck(element).on("ifChanged",function(){$(this).resetValid()});
				}
			}
		},
		{header:'${text("区块匹配规则")}', name:'rectangFilterExpression', width:100, 
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], "#{@DictUtils.getDictListJson('ocr_rectang_filter_expression')}"),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					js.select2(element).on("change",function(){$(this).resetValid()});
				}
			}
		},
		{header:'${text("文字匹配规则")}', name:'textFilterExpression', width:100, 
			editable:true, edittype:'select', editoptions:{'class':'form-control',
				items: $.merge([{dictLabel:'&nbsp;',dictValue:''}], "#{@DictUtils.getDictListJson('ocr_check_rectang_text_filter_expression')}"),
				itemLabel: 'dictLabel', itemValue: 'dictValue', dataInit: function(element){
					js.select2(element).on("change",function(){$(this).resetValid()});
				}
			}
		},
		{header:'${text("块内文字")}', name:'checkRectangText', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'500', 'class':'form-control '}},
		{header:'${text("权重")}', name:'weight', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control digits'}},
		{header:'${text("操作")}', name:'actions', width:80, align:'center', formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#ocrTempateConfigsDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				actions.push('<a href="#" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#ocrTempateConfigsDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}}
	],
	
	//# // 编辑表格参数
	editGrid: true,				// 是否是编辑表格
	editGridInitRowNum: 1,		// 编辑表格的初始化新增行数
	editGridAddRowBtn: $('#ocrTempateConfigsDataGridAddRowBtn'),	// 子表增行按钮
	editGridAddRowBtnToHeader: true,	// 子表增行按钮是否显示到表头上
	editGridAddRowInitData: {ocrTempateConfigsId: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

	//# // 编辑表格的提交数据参数
	editGridInputFormListName: 'ocrTempateConfigsList', // 提交的数据列表名
	editGridInputFormListAttrs: 'status,ocrTempateConfigsId,ocrTemplateId.ocrTemplateId,checkItems,ocrNo,sort,ocrText,charScores,crnnTime,blockTime,boxScore,angleIndex,angleScore,angleTime,boxPoint,rectangFilterExpression,textFilterExpression,checkRectangText,', // 提交数据列表的属性字段
	
	//# // 加载成功后执行事件
	ajaxSuccess: function(data){
		
	}
});
</script>
<script>
$("#inputForm").validate({
	submitHandler: function(form){
		js.ajaxSubmitForm($(form), function(data){
			js.showMessage(data.message);
			if(data.result == Global.TRUE){
				js.closeCurrentTabPage(function(contentWindow){
					contentWindow.page();
				});
			}
		}, "json");
    }
});

function deleteALLGrid(){
	
	js.confirm('你确认要清空特征规则数据吗，一旦清空，该操作不可逆？', function(){
		deleteAllList();
	});return false;
	

}

function deleteAllList(){
	var allRowData=$('#ocrTempateConfigsDataGrid').dataGrid('getDataIDs');
			for (var i = 0; i < allRowData.length; i++) {
			    var rowId = allRowData[i];
				$('#ocrTempateConfigsDataGrid').dataGrid('setRowData',rowId,null,{display:'none'});$('#'+rowId+'_status').val('1');
			}
	$("#imageToRecImageDiv").html("");
}

function addRowTemplateRule(){
	deleteAllList();
	var tempateId=$("#ocrTemplateId").val();
	var tempateImageId=$("#tempateImageId").val();
	if (tempateImageId==null||''==tempateImageId) {js.alert("暂无模板，请上传模板后再解析内容！");return;} 
	var tempateImage=$("#tempateImage").val();
	js.ajaxSubmitJson({
	   url: '${ctx}/ocr/ocrTemplate/ocrRecImage', data: {id:tempateId,imageId:tempateImageId,tempateImage:tempateImage},
	        callback: function(data){
			log(data.ocrTempateConfigsList)
			if("true"!=data.result) {js.alert(data.message);return;}
			var tempateImageRec=data.tempateImageRec;
			var innerHtml=js.template('imageToRecImage', {ocrTempateConfigsId: '123123',tempateImage:tempateImage,tempateImageRec:tempateImageRec,status: Global.STATUS_NORMAL});
			$("#imageToRecImageDiv").html(innerHtml);
			// 使用setGridParam更新jqGrid的数据
			$('#ocrTempateConfigsDataGrid').jqGrid('setGridParam', {
			    datatype: 'local',
			    data: data.ocrTempateConfigsList
			}).trigger('reloadGrid');			
		}, dataType: 'json',
	async: true, message: '正在解析图片内容...'});
}

function fileuploadCallback(id, act, $this, fileUploadId, fileUrl, fileName, fileUpload){
	if(fileUploadId === undefined || fileUploadId === null||""===fileUploadId){$("#tempateImageId").val($('#uploadImagefileLists li:first').attr('id'));}
	else{$("#tempateImageId").val(fileUploadId);}
}
</script>

<script id="imageToRecImage" type="text/template">//<!--
	<div class="col-xs-6 ">
		<picture>
		<img class="img-zoom img-fluid img-thumbnail " src="{{d.tempateImage}}"/>
		</picture>
	</div>
	<div class="col-xs-6 ">
		<picture>
		<img class="img-zoom img-fluid img-thumbnail " src="{{d.tempateImageRec}}"/>
		</picture>
	</div>	
//--></script>

<style>
		.img-zoom {
		  transition: transform .2s; /* Animation */
		  height: 100%;
		  width: 100%;
		}

		.img-zoom:hover {
		  transform: scale(1.5); /* (150% zoom - Note: 1.1 will zoom it up 10% */
		}
</style>	
